/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.krArmor;

import java.util.Collection;
import java.util.Map;
import ru.jSkyRoom.generator.builder.ruleBuilder.EnchantmentionGeneratorRuleBuilder;
import ru.jSkyRoom.generator.command.rule.Rule;
import ru.jSkyRoom.interfaces.major.Armor;
import ru.jSkyRoom.interfaces.major.Enchantment;
import ru.jSkyRoom.interfaces.JSkyMod;
import ru.jSkyRoom.support.MajorEdidParser;
import ru.jSkyRoom.support.RomeNumber;
import ru.rakev.matcher.Matcher;
import ru.rakev.matcher.factory.MatcherDecoratorFactory;

/**
 *
 * @author Rakev
 */
public class EnchanterRuleCreator {

    private JSkyMod creatingMod;
    private Enchantment enchantment;
    private Map<String, Matcher<Armor>> enchantmentEdidRegexToPermitedKeywordRegexLink;
    private Map<String, Matcher<Armor>> enchantmentEdidRegexToForbidenKeywordRegexLink;
    private Map<String, String> enchantmentEdidRegexToNameAppendixLink;
    private Collection<String> enchantmentEdidRegexToLevelingName;

    public EnchanterRuleCreator(JSkyMod creatingMod, Enchantment enchantment, Map<String, Matcher<Armor>> enchantmentEdidRegexToPermitedKeywordRegexLink, Map<String, Matcher<Armor>> enchantmentEdidRegexToForbidenKeywordRegexLink, Map<String, String> enchantmentEdidRegexToNameAppendixLink, Collection<String> enchantmentEdidRegexToLevelingName) {
        this.creatingMod = creatingMod;
        this.enchantment = enchantment;
        this.enchantmentEdidRegexToPermitedKeywordRegexLink = enchantmentEdidRegexToPermitedKeywordRegexLink;
        this.enchantmentEdidRegexToForbidenKeywordRegexLink = enchantmentEdidRegexToForbidenKeywordRegexLink;
        this.enchantmentEdidRegexToNameAppendixLink = enchantmentEdidRegexToNameAppendixLink;
        this.enchantmentEdidRegexToLevelingName = enchantmentEdidRegexToLevelingName;
    }

    public Rule<Armor> create() {
        EnchantmentionGeneratorRuleBuilder<Armor> ruleBuilder = new EnchantmentionGeneratorRuleBuilder(creatingMod, enchantment);
        ruleBuilder = addPermitRuleMatcher(ruleBuilder);
        ruleBuilder = addForbidenRuleMatcher(ruleBuilder);
        ruleBuilder = addNameAppendix(ruleBuilder);
        ruleBuilder = addRangAppendix(ruleBuilder);
        return ruleBuilder.build();

    }

    private EnchantmentionGeneratorRuleBuilder<Armor> addPermitRuleMatcher(EnchantmentionGeneratorRuleBuilder<Armor> ruleBuilder) {
        for (String enchantmentRegex : enchantmentEdidRegexToPermitedKeywordRegexLink.keySet()) {
            if (enchantment.getEDID().matches(enchantmentRegex)) {
                Matcher<Armor> permitMatcher = enchantmentEdidRegexToPermitedKeywordRegexLink.get(enchantmentRegex);
                ruleBuilder.addRuleMatcher(permitMatcher);
            }
        }
        return ruleBuilder;
    }

    private EnchantmentionGeneratorRuleBuilder<Armor> addForbidenRuleMatcher(EnchantmentionGeneratorRuleBuilder<Armor> ruleBuilder) {
        for (String enchantmentRegex : enchantmentEdidRegexToForbidenKeywordRegexLink.keySet()) {
            if (enchantment.getEDID().matches(enchantmentRegex)) {
                Matcher<Armor> forbidenMatcher = enchantmentEdidRegexToForbidenKeywordRegexLink.get(enchantmentRegex);
                ruleBuilder.addRuleMatcher(MatcherDecoratorFactory.create().not(forbidenMatcher));
            }
        }
        return ruleBuilder;
    }

    private EnchantmentionGeneratorRuleBuilder<Armor> addNameAppendix(EnchantmentionGeneratorRuleBuilder<Armor> ruleBuilder) {
        for (String enchantmentEdidRegex : enchantmentEdidRegexToNameAppendixLink.keySet()) {
            if (enchantment.getEDID().matches(enchantmentEdidRegex)) {
                String nameAppendix = enchantmentEdidRegexToNameAppendixLink.get(enchantmentEdidRegex);
                ruleBuilder.addNameAppendix(nameAppendix);
            }
        }
        return ruleBuilder;
    }

    private EnchantmentionGeneratorRuleBuilder<Armor> addRangAppendix(EnchantmentionGeneratorRuleBuilder<Armor> ruleBuilder) {
        for (String enchantmentEdidRegex : enchantmentEdidRegexToLevelingName) {
            if (enchantment.getEDID().matches(enchantmentEdidRegex)) {
                int enchantmentRang = new MajorEdidParser(enchantment.getEDID()).parseRang();
                String nameAppendix = RomeNumber.parse(enchantmentRang).name();
                ruleBuilder.addNameAppendix(nameAppendix);
            }
        }
        return ruleBuilder;
    }
}
